#include<string>
#include<iostream>
using namespace std;
class Solution {
public:
    void reverse(string::iterator left, string::iterator right)
    {
        while (left < right)
        {
            char tmp = *left;
            *left = *right;
            *right = tmp;
            left++;
            right--;
        }
    }
    string reverseWords(string s)
    {
        string::iterator left = s.begin();
        string::iterator right = s.begin();
        string::iterator begin = s.begin();

        size_t pos = 0;
        while ((pos = s.find(" ", pos)) != string::npos)
        {
            right = begin + pos - 1;
            reverse(left, right);
            begin = s.begin();
            left = begin + pos + 1;
            ++pos;
        }
        if (s.size() > 1)
        {
            right = s.end() - 1;
            reverse(left, right);
        }
        return s;
    }
};